package com.fqcar.fqdf.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SharedPreferenceUtil {

    private SharedPreferenceUtil() {
    }

    public static String getStringSPAttrs(Context context, AttrInfo spInfo, String defaultValue) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(spInfo.spName, Context.MODE_PRIVATE);
        return sharedPreferences.getString(spInfo.attrName, defaultValue);
    }

    public static void setStringSPAttrs(Context context, AttrInfo spInfo, String value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(spInfo.spName, Context.MODE_PRIVATE);
        sharedPreferences.edit().putString(spInfo.attrName, value).commit();
    }

    public static boolean getBooleanSPAttrs(Context context, AttrInfo spInfo, boolean defaultValue) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(spInfo.spName, Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(spInfo.attrName, defaultValue);
    }

    public static boolean getBooleanSPAttrs(Context context, AttrInfoCustom spInfo, boolean defaultValue) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(spInfo.spName, Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(spInfo.attrName, defaultValue);
    }

    public static void setBooleanSPAttrs(Context context, AttrInfoCustom spInfo, boolean value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(spInfo.spName, Context.MODE_PRIVATE);
        sharedPreferences.edit().putBoolean(spInfo.attrName, value).commit();
    }

    public static void setBooleanSPAttrs(Context context, AttrInfo spInfo, boolean value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(spInfo.spName, Context.MODE_PRIVATE);
        sharedPreferences.edit().putBoolean(spInfo.attrName, value).commit();
    }

    public static int getIntSPAttrs(Context context, AttrInfo spInfo, int defaultValue) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(spInfo.spName, Context.MODE_PRIVATE);
        return sharedPreferences.getInt(spInfo.attrName, defaultValue);
    }

    public static void setIntSPAttrs(Context context, AttrInfo spInfo, int value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(spInfo.spName, Context.MODE_PRIVATE);
        sharedPreferences.edit().putInt(spInfo.attrName, value).commit();
    }

    public static Long getLongSPAttrs(Context context, AttrInfo spInfo, Long defaultValue) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(spInfo.spName, Context.MODE_PRIVATE);
        return sharedPreferences.getLong(spInfo.attrName, defaultValue);
    }

    public static void setLongSPAttrs(Context context, AttrInfo spInfo, Long value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(spInfo.spName, Context.MODE_PRIVATE);
        sharedPreferences.edit().putLong(spInfo.attrName, value).commit();
    }

    public static void clearSP(Context context, String spName) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
        sharedPreferences.edit().clear().commit();
    }

    public static final String SP_NAME_PROPS = "props";
    public static final String SP_NAME_USER = "user_info";
    public static final String SP_NAME_OPEARTE = "opearte";
    //    public static final String SP_PROP_WXPAY_RESULT_CODE = "wxPayResultcode";
//    public static final String SP_PROP_PAY_FOR_TYPE = "pay_for_type";//支付for type(订单支付，违章支付，充值)
    public static final String SP_PROP_ORDER_ID = "orderId";
    public static final String SP_PROP_USING_CAR_TIP = "using_car_tip";//驾驶手册的提示
    public static final String SP_PROP_WAITING_USE_CAR_TIP = "waiting_use_car_tip";//
    public static final String SP_PROP_POWER_USE_CAR_TIP = "power_use_car_tip";//
    public static final String SP_PROP_PUSH_STATE_HD = "push_state_hd";//
    public static final String SP_PROP_PUSH_STATE_XT = "push_state_xt";//
    public static final String SP_PROP_PUSH_STATE_ZX = "push_state_zx";//
    public static final String SP_PROP_FIRST_LOGIN = "first_login";//
    public static final String NOTIFICATION_SWITCH = "false";//开启通知开关
    public static final String PUSH_USER_CHECK = "false";//提交双证审核
    public static final String USER_QUALIFCATION_CERTIFICATION= "false";//资质认证

    public enum AttrInfo {
        PUSH_QUALIFCATION_ID(USER_QUALIFCATION_CERTIFICATION, "false"),
        PUSH_USRR_CHECK_ID(PUSH_USER_CHECK, "false"),
        PUSH_SWITCH(NOTIFICATION_SWITCH, "false"),
        PROP_FIRST_USE(SP_NAME_PROPS, "first_use"), //是否首次登陆
        PROP_FIRST_LOGIN(SP_PROP_FIRST_LOGIN, "no"), //是否之前登陆过
        PROP_GT_PUSH_CLIENT_ID(SP_NAME_PROPS, "gt_push_token"),//个推push_token
        PROP_GT_PUSH_MESSAGE_STATE_HD(SP_PROP_PUSH_STATE_HD, "msg_hd"),//活动消息是否已读
        PROP_GT_PUSH_MESSAGE_STATE_ZX(SP_PROP_PUSH_STATE_ZX, "msg_zx"),//消息中心是否已读
        PROP_GT_PUSH_MESSAGE_STATE_XT(SP_PROP_PUSH_STATE_XT, "msg_xt"),//系统消息是否已读
        PROP_NAV_RETURN_PARKING_NAME(SP_NAME_PROPS, "return_parkingName"),
        USER_HAS_LOGIN(SP_NAME_USER, "hasLogin"), //是否登录
        USER_TOKEN(SP_NAME_USER, "token"),//token
        USER_ID(SP_NAME_USER, "userId"),//token
        USER_NICKNAME(SP_NAME_USER, "nickName"),//昵称
        USER_SEX(SP_NAME_USER, "sex"),//性别
        USER_HEAD_IMG(SP_NAME_USER, "headImg"),//头像
        USER_PHONE(SP_NAME_USER, "phone"),//手机号码
        USER_CREDIT_STATE(SP_NAME_USER, "creditState"),//信用卡绑定状态
        //        USER_PAY_FOR_TYPE(SP_PROP_PAY_FOR_TYPE, "pay_for_type"),//支付for type(订单支付，违章支付，充值)
        USING_CAR_TIP(SP_PROP_USING_CAR_TIP, "using_car_tip"),
        WAITING_USE_CAR_TIP(SP_PROP_WAITING_USE_CAR_TIP, "waiting_use_car_tip"),
        POWER_USE_CAR_TIP(SP_PROP_POWER_USE_CAR_TIP, "power_use_car_tip"),
        USER_ORDER_ID(SP_PROP_ORDER_ID, "orderId"),//orderId
        PROP_FIRST_RECOMMEND(SP_NAME_PROPS, "first_recommend"), //是否再次显示推荐建点提示
        PROP_FIRST_CHARGE(SP_NAME_PROPS, "first_charge"), //是否再次显示充电帮助
        PROP_FIRST_NIGHT(SP_NAME_PROPS, "first_night");//是否再次显示夜租规则提示
//        PROP_WXPAY_RESULT_CODE(SP_PROP_WXPAY_RESULT_CODE, "wxPayResultcode");//微信支付回调code

        public String spName;
        public String attrName;

        AttrInfo(String spName, String attrName) {
            this.spName = spName;
            this.attrName = attrName;
        }
    }

    public static final String NIGHT_RULE_CITY = "night_rule_city";

    public static final String FUN_RULE_CITY = "fun_rule_city";

    /**
     * 设置自定义的sp内容
     */
    public static class AttrInfoCustom {

        private String spName;

        private String attrName;

        public AttrInfoCustom(String spName, String attrName) {
            this.spName = spName;
            this.attrName = attrName;
        }
    }

    /**
     * 把对象保存到以String形式保存到sp中
     *
     * @param context 上下文
     * @param object  泛型参数
     * @param
     */
    public static void saveObj2SP(Context context, Object object) {

//		SharedPreferences preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
        ByteArrayOutputStream bos;
        ObjectOutputStream oos = null;
        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(object);
            byte[] bytes = bos.toByteArray();
            String ObjStr = Base64.encodeToString(bytes, Base64.DEFAULT);
//			SharedPreferences.Editor editor = preferences.edit();
//			editor.putString(keyName, ObjStr);
//			editor.commit();
            SharedPreferenceUtil.setStringSPAttrs(context, AttrInfo.PROP_NAV_RETURN_PARKING_NAME, ObjStr);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
//关闭流
            if (oos != null) {
                try {
                    oos.flush();
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }

    }

    /**
     * sp文件名 和 字段名相同
     * @param context
     * @param t
     * @param spName
     * @param <T>
     */
    /*public static <Object> void saveObj2SP(Context context, Object t) {
        saveObj2SP(context, t);
	}*/


    /**
     * 从sp中读取对象
     *
     * @param context
     * @param spName  sp文件名
     * @param keyNme  字段名
     * @param <T>     泛型参数
     * @return
     */
    public static <T> T getObjFromSp(Context context, String spName, String keyNme) {
        SharedPreferences preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
        byte[] bytes = Base64.decode(preferences.getString(keyNme, ""), Base64.DEFAULT);
        ByteArrayInputStream bis;
        ObjectInputStream ois = null;
        T obj = null;
        try {
            bis = new ByteArrayInputStream(bytes);
            ois = new ObjectInputStream(bis);
            obj = (T) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return obj;
    }

    public static <T> T getObjFromSp(Context context, String spName) {
        return getObjFromSp(context, spName, spName);
    }
}
